require("./login.css");

//引入验证码插件
let Captcha = require("captcha-mini");
//引入axios
const axios = require("axios");
//
window.addEventListener("load", function () {
	// 封装获取的函数
	function $(v) {
		return document.querySelector(v);
	}
	let auth = "";
	// 验证码
	let captcha1 = new Captcha();
	captcha1.draw(document.querySelector("#captcha1"), (r) => {
		auth = r.toLowerCase();
	});
	//切换到密码登录
	$("#enter").addEventListener("click", function () {
		location.href = "./log-in.html";
	});
	//

	// 表单验证
	$("button").addEventListener("click", function () {
		let phonenmb = $("#phone-nmb").value;
		let password = $("#password").value;
		console.log(phonenmb);
		//手机号验证
		let a = /^1[345678]\d{9}$/;
		if (a.test(phonenmb)) {
		} else {
			alert("手机号错误");
			return;
		}
		// 验证码验证
		let verify = $("#verify").value;
		if (verify.toLowerCase() === auth) {
		} else {
			alert("验证码错误");
			return;
		}
		// 密码验证
		console.log(password.length);
		if (password.length >= 6 && password.length <= 12) {
		} else {
			alert("密码需要6至12位");
			return;
		}
		// 再次验证
		if ($("#notarize").value === $("#password").value) {
		} else {
			alert("两次密码不一致");
			return;
		}
		//验证都没有问题，发送请求
		axios
			.post("http://fitness.h5.itsource.cn:3701/api/user/register", {
				account: phonenmb,
				password: password,
			})
			.then((v) => {
				if (v.data.errno === 0) {
					alert("注册成功");
					location.href = "./log-in.html";
				} else {
					alert("注册失败，" + v.data.message);
				}
			});
	});
});
